package com.spark.gaea.mvc.utils;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.CharArrayWriter;
import java.io.PrintWriter;

public class ResponseWrapper extends HttpServletResponseWrapper {
	private PrintWriter cachedWriter;
	private CharArrayWriter bufferedWriter;

	public ResponseWrapper(final HttpServletResponse response) {
		super(response);
		this.bufferedWriter = new CharArrayWriter();
		this.cachedWriter = new PrintWriter(this.bufferedWriter);
	}

	@Override
	public PrintWriter getWriter() {
		return this.cachedWriter;
	}

	@Override
	public void finalize() throws Throwable {
		super.finalize();
		this.cachedWriter.close();
		this.bufferedWriter.close();
	}

	public String getContent() {
		return this.bufferedWriter.toString();
	}
}
